function[Mhat] = plot_estimation_results2(input_mat_file, retailer, side, extension)

version=1;
load(input_mat_file)

global folderfig day homedealer keepall quantityrobus largetrade tradesize  fej


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%SHOW RESULTS 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if extension==1
    Mhat = Mhat0;
    nanmedian(Mhat(:, [1:2, cols+2]))
end 

%Table: median day, averaged across dealers
if extension==0
    nanmedian(Mhat)
    nanmean(Mhat)
    nanmedian(SE)
end 


if retailer==0
    if side==1
        nanmedian(vdjSQ(:,cols)-Y(new_period(:,2),5))
    else
        nanmedian(Y(new_period(:,2),5)-vdjSQ(:,cols))
    end 
    
mean(nanmean(etaEjSQ(etaEjSQ~=-Inf)))
mean(nanmean(etaDjSQ))
mean(nanmedian(xij_data))

mean(nanmean(xij_data))
mean(nanmedian(vdjSQ(:,cols)))


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% PLOTS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%sort according to dealers: 46
xij = xij_data; %base quality
med_xij=nanmedian(xij_data);
cols = [1:9];


%%Dealer quality: xij 
figure 
[xij_ro] = rmoutliers(xij(:,cols),'percentiles',[1,99]);
%boxplot(xij(:,cols), 'Label',  {'d1', 'd2', 'd3',  'd4', 'd0','d5', 'd6', 'd7','d8'},'Symbol','+k', 'Color', 'k')
boxplot(xij(:,cols), 'Label',  {'d1', 'd2', 'd3',  'd4', 'd0','d5', 'd6', 'd7','d8'},'Symbol','+k', 'Color', 'k')
ylabel('basis points')
%set(gca,'FontSize',16, 'XTickLabelRotation',30)
ylim([-4 4])
xlim=get(gca,'xlim');
hold on
plot(xlim,[0 0],'LineStyle','-.', 'Color', 'k')
set(gca,'FontSize',22)
set(gcf,'color','w');
temp=['/graph_xi_avg',['_',num2str(side),'_' num2str(retailer),'_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize), '_', num2str(fej)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))


if homedealer==1
    
%Platform market share split into home dealer and dealer
for j=1:9
    sNHjSQ_total(:,j)=nansum(sNHjSQ(:,:,j),2);
    s0NHjSQ_total(:,j)=nansum(s0NHjSQ(:,:,j),2);
end 

%stack = [s0HjSQ(:,1), s0NHjSQ_total(:,1), ...
%         s0HjSQ(:,2), s0NHjSQ_total(:,2), ...
%         s0HjSQ(:,3), s0NHjSQ_total(:,3), ...
%         s0HjSQ(:,4), s0NHjSQ_total(:,4), ...
%         s0HjSQ(:,5), s0NHjSQ_total(:,5), ...
%         s0HjSQ(:,6), s0NHjSQ_total(:,6), ...
%         s0HjSQ(:,7), s0NHjSQ_total(:,7),  ...
%         s0HjSQ(:,8), s0NHjSQ_total(:,8),  ...
%         s0HjSQ(:,9), s0NHjSQ_total(:,9)];
     
%stack_ro=rmoutliers(stack, 'percentiles',[1,99]);     
%figure
%boxplot(stack_ro, 'Label', {'18H', '18NH', '24H', '24NH', '46H', '46NH', '61H','61NH', '67H','67NH','77H','77NH', '88H', '88NH','93H','93NH','94H', '94NH'},'Symbol','+k', 'Color', 'k')
%ylabel('basis points')
%set(gca,'FontSize',20)
%set(gcf,'color','w');
%title('platform market shares (conditional on entry)')
%temp=['/graph_sj',['_',num2str(side),'_' num2str(retailer),'_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize)]]; 
%saveas(gcf,fullfile([folderfig,temp,'.png']))


figure
boxplot(s0HjSQ, 'Label', {'d1', 'd2', 'd3',  'd4', 'd0','d5', 'd6', 'd7','d8'},'Symbol','+k', 'Color', 'k')
ylabel('mass')
set(gca,'FontSize',20)
set(gcf,'color','w');
temp=['/graph_sjH',['_',num2str(side),'_' num2str(retailer),'_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize), '_', num2str(fej)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))

figure
boxplot(rmoutliers(s0NHjSQ_total, 'percentiles',[1,99]), 'Label',  {'d1', 'd2', 'd3',  'd4', 'd0','d5', 'd6', 'd7','d8'},'Symbol','+k', 'Color', 'k')
ylabel('mass')
set(gca,'FontSize',20)
xlim=get(gca,'xlim');
ylim([0 2])
hold on
plot(xlim,[median(s0NHjSQ_total(:,5) +0.001) median(s0NHjSQ_total(:,5)+0.001)],'LineStyle','-.', 'Color', 'k')
set(gcf,'color','w');
temp=['/graph_sjNH',['_',num2str(side),'_' num2str(retailer),'_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize), '_', num2str(fej)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))


figure 
boxplot([nanmedian(s0HjSQ,2), nanmedian(s0NHjSQ_total,2)], 'Label', {'home investors', 'non home investors'},'Symbol', '+k', 'Color', 'k')
ylabel('mass')
ylim([0 1])
set(gca,'FontSize',20)
set(gcf,'color','w');
temp=['/graph_sj_medD',['_',num2str(side),'_' num2str(retailer),'_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize), '_', num2str(fej)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))


%Platform market share per dealer conditional on entry
figure
[sj0_data_ro] = rmoutliers(s0j_data(:,cols), 'percentiles',[1,99]);
boxplot(sj0_data_ro, 'Label',{'d1', 'd2', 'd3',  'd4', 'd0','d5', 'd6', 'd7','d8'},'Symbol','+k', 'Color', 'k')
ylabel('')
set(gca,'FontSize',20)
xlim=get(gca,'xlim');
hold on
plot(xlim,[median(s0j_data(:,5)) median(s0j_data(:,5))],'LineStyle','-.', 'Color', 'k')
set(gcf,'color','w');
set(gca,'FontSize',20)
temp=['/graph_sj',['_',num2str(side),'_' num2str(retailer),'_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize), '_', num2str(fej)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))


%Probability to trade with a dealer other than the homedealer
figure
boxplot([nanmedian((1-rhojSQ).*s0NHjSQ_total,2)*100], 'Label', {'model'} ,'Symbol','+k', 'Color', 'k')
ylabel('%')
set(gca,'FontSize',20)
set(gcf,'color','w')   
end 


%Probability to enter the platform for median dealer - works more or less
figure
boxplot([nanmean(rhoj_data,2), nanmean(rhojSQ,2)], 'Label', {'data', 'model'} ,'Symbol','+k', 'Color', 'k')
ylabel('pp')
%bp.XAxis.TickLabelInterpreter = 'latex';
set(gca,'FontSize',20)
ylim([0 1])
set(gcf,'color','w');
temp=['/graph_rhoj_avg',['_',num2str(side),'_' num2str(retailer),'_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize), '_', num2str(fej)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))


%%Platform elasticity
figure 
subplot(1,2,1)
[elaDj_ro] = rmoutliers(etaDjSQ(:,cols), 'percentiles',[1,99]);
boxplot(elaDj_ro, 'Label', {'d1', 'd2', 'd3',  'd4', 'd0','d5', 'd6', 'd7','d8'},'Symbol','+k', 'Color', 'k')
ylabel('basis points')
set(gca,'FontSize',20)
title('etaD')
subplot(1,2,2)
[elaEj_ro] = rmoutliers(etaEjSQ(:,cols), 'percentiles',[1,99]);
boxplot(elaEj_ro, 'Label', {'d1', 'd2', 'd3',  'd4', 'd0','d5', 'd6', 'd7','d8'},'Symbol','+k', 'Color', 'k')
ylabel('basis points')
set(gca,'FontSize',20)
title('etaE')
temp=['/graph_elaE_avg',['_',num2str(side),'_' num2str(retailer),'_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize), '_', num2str(fej)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))


%%Dealer valuations
figure 
[vdjSQ_ro] = rmoutliers(vdjSQ(:,cols), 'percentiles',[1,99]);
boxplot(vdjSQ_ro, 'Label',{'d1', 'd2', 'd3',  'd4', 'd0','d5', 'd6', 'd7','d8'},'Symbol','+k', 'Color', 'k')
ylabel('basis points')
set(gca,'FontSize',20)
temp=['/graph_valuej_avg',['_',num2str(side),'_' num2str(retailer),'_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize), '_', num2str(fej)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))

%over theta
figure 
if side==1
    [vdjSQ_ro] = rmoutliers(vdjSQ(:,cols)-Y(new_period(:,2),5),'percentiles',[1,99]);
elseif side==-1
    [vdjSQ_ro] = rmoutliers(Y(new_period(:,2),5)-vdjSQ(:,cols),'percentiles',[1,99]);
end 
boxplot(vdjSQ_ro, 'Label',{'d1', 'd2', 'd3',  'd4', 'd0','d5', 'd6', 'd7','d8'},'Symbol','+k', 'Color', 'k')
ylabel('basis points')
ylim([-5 5])
xlim=get(gca,'xlim');
hold on
plot(xlim,[0 0],'LineStyle','-.', 'Color', 'k')
set(gca,'FontSize',20, 'color','w');
temp=['/graph_mcj_avg',['_',num2str(side),'_' num2str(retailer),'_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize), '_', num2str(fej)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png'])) 


%Dealer valuations and quotes
d1=4; d2=6;
figure 
scatter((vdjSQ(:,cols(d1))-vdjSQ(:,cols(d2))), (q_data(:,cols(d1)).*(1+1./etaEjSQ(:,cols(d1)))-q_data(:,cols(d2)).*(1+1./etaEjSQ(:,cols(d2)))), [], [0.3 0.3 0.3 ], MarkerFaceAlpha = 0.8)
hold on 
scatter((vdjSQ(:,cols(d1))-vdjSQ(:,cols(d2))), (q_data(:,cols(d1))-q_data(:,cols(d2))), [], [0.1 0.1 0.8 ], MarkerFaceAlpha = .8, Marker="+")
xlabel('value of dealer 1 - value of dealer 2')
ylabel('quote of dealer 1 - quote of dealer 2')
set(gca,'FontSize',20)
set(gcf, 'color','w');
temp=['/graph_valueD_quotes',['_',num2str(side),'_' num2str(retailer),'_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize), '_', num2str(fej)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))


%Dealer valuations boxplots 
time=[(T-10):T];
valuebase = vdjSQ-Y(new_period(:,2),5);
v_box =[valuebase(time(1),:)', valuebase(time(2),:)',  valuebase(time(3),:)',  valuebase(time(4),:)',  valuebase(time(5),:)',valuebase(time(6),:)', valuebase(time(7),:)',  valuebase(time(8),:)',  valuebase(time(9),:)',  valuebase(time(10),:)'];
figure
boxplot(v_box, 'Labels', {'week 1', '', '', '', 'week 5','', '', '', '', 'week 10'}, 'Colors','k', 'OutlierSize',0.001)
xtickangle(0)
hold on 
plot(v_box','o','MarkerEdgeColor','k')
set(gca,'FontSize',20)
set(gcf, 'color','w');
temp=['/graph_valueD_dispersion',['_',num2str(side),'_' num2str(retailer),'_',num2str(day), '_', num2str(homedealer), '_', num2str(keepall),'_', num2str(quantityrobus),'_', num2str(largetrade), '_', num2str(tradesize), '_', num2str(fej)]]; 
saveas(gcf,fullfile([folderfig,temp,'.png']))

end 


end


